import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ty_smart_watch/bean/body_data_bean.dart';
import 'package:ty_smart_watch/common/function_id.dart';
import 'package:ty_smart_watch/page/smartlife/controller/body_grid_controller.dart';
import 'package:ty_smart_watch/route/routes.dart';

class BodyGrid extends StatelessWidget {
  final Function(int)? onPageChanged;

  BodyGridController controller = Get.find();

  BodyGrid({super.key, required this.onPageChanged});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(10, 10, 10, 10),
      child: Obx(
        () => GridView.builder(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2, childAspectRatio: 1.5, mainAxisSpacing: 10, crossAxisSpacing: 10),
          itemCount: controller.list.length,
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                navTo(controller.list[index].id);
              },
              child: BodyDataItem(bodyData: controller.list[index]),
            );
          },
        ),
      ),
    );
  }

  navTo(int id) {
    switch (id) {
      case HEART:
      case BLOOD_GLUCOSE:
      case BLOOD_OXYGEN:
      case BLOOD_PRESSURE:
      case BODY_TEMPERATURE:
        Get.toNamed(Routes.heartChartPage, arguments: {"id": id});
      case SLEEP:
        Get.toNamed(Routes.sleepChartPage);
      case SPORT_RECORDER:
        if (onPageChanged != null) {
          onPageChanged!(1);
        }
        break;
    }
  }
}

class BodyDataItem extends StatelessWidget {
  const BodyDataItem({super.key, required this.bodyData});

  final BodyDataBean bodyData;

  @override
  Widget build(BuildContext context) {
    BoxDecoration decoration = BoxDecoration(
      //圆角半径
      borderRadius: const BorderRadius.all(Radius.circular(12.0)),
      color: Color(bodyData.color),
    );

    return Container(
      padding: const EdgeInsets.all(10),
      decoration: decoration,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Image.asset(bodyData.icon, width: 20, height: 20),
          Text(bodyData.title, style: const TextStyle(fontSize: 12)),
          Container(height: 20),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                bodyData.data,
                style: const TextStyle(fontSize: 20, height: 1.2),
              ),
              Container(width: 6),
              Text(bodyData.unit)
            ],
          )
        ],
      ),
    );
  }
}
